﻿
Public Class ZipperProgressViewer
    Implements IDisposable
    'Load :
    Private Sub ZipProgressViewer_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Control.CheckForIllegalCrossThreadCalls = False
        ResetProgressFields()
        KH_Controls.Enabled = False
    End Sub
    'Dispose :
    Public Sub DisposeFromAll()
        _ZipMaker.Dispose()
        Me.Dispose()
    End Sub
    Private _ZDesigner As ZipperDesigner
    Public Property ZDesigner As ZipperDesigner
        Get
            Return _ZDesigner
        End Get
        Set(value As ZipperDesigner)
            _ZDesigner = value
        End Set
    End Property
    'Zip Operations :
    Private WithEvents _ZipMaker As ZipMaker
    Private _SourceZipNode As ZipNode
    Private _ZipLocation As String
    'Reset Progress Fields :
    Private Sub ResetProgressFields()
        ZipHeader.Values.Image = Nothing
        ZipHeader.Values.Heading = ""
        ZipHeader.Values.Description = ""
        ZipPB.Value = 0

        EntryHeader.Values.Image = Nothing
        EntryHeader.Values.Heading = ""
        EntryHeader.Values.Description = ""
        EntryPB.Value = 0

        KL_StartTime.Text = TimeSpan.Zero.ValueString
        KL_EllapsedTime.Text = TimeSpan.Zero.ValueString
        KL_RemainingTime.Text = TimeSpan.Zero.ValueString

        KL_SavedEntries.Text = CStr(0)
        KL_TotalEntries.Text = CStr(0)
        KL_ProgressSpeed.Text = StringFromSize(0.0) & " / S"
        KL_SavedSize.Text = StringFromSize(0.0)
        KL_TotalSize.Text = StringFromSize(0.0)
    End Sub
    'Show Progress Fields :
    Private Sub ShowZipHeading()
        ZipHeader.Values.Image = _SourceZipNode.Type.ZipImage
        ZipHeader.Values.Heading = _SourceZipNode.Text
    End Sub
    Private Sub ShowZipProgress()
        Dim percent As Double = _ZipMaker.ZipPercent
        ZipHeader.Values.Description = String.Format("{0,4:P02}", percent)
        ZipPB.Value = CInt(Math.Ceiling(percent * 100))
    End Sub
    Private Sub ShowEntryHeading(ByVal img As Image, ByVal text As String)
        EntryHeader.Values.Image = img
        EntryHeader.Values.Heading = text
    End Sub
    Private Sub ShowEntryProgress()
        Dim percent As Double = _ZipMaker.EntryPercent
        EntryHeader.Values.Description = String.Format("{0,4:P02}", percent)
        EntryPB.Value = CInt(Math.Ceiling(percent * 100))
    End Sub
    Private Sub ShowStartingFields()
        KL_StartTime.Text = _ZipMaker.StartTime.ValueString
        KL_TotalEntries.Text = CStr(_ZipMaker.EntriesTotal)
        KL_TotalSize.Text = StringFromSize(_ZipMaker.ZipTotalSize)
    End Sub
    Private Sub ShowProgressFields()
        KL_EllapsedTime.Text = _ZipMaker.EllapsedTime.ValueString
        KL_RemainingTime.Text = _ZipMaker.RemainingTime.ValueString
        KL_ProgressSpeed.Text = StringFromSize(_ZipMaker.ProgressSpeed) & "/S"
        KL_SavedEntries.Text = CStr(_ZipMaker.EntriesWritten)
        KL_SavedSize.Text = StringFromSize(_ZipMaker.ZipWrittenSize)
    End Sub
    'Crate Zip :
    Public Sub CreateZip(ByVal location As String, ByVal filters As List(Of Filter))
        ResetProgressFields()
        _SourceZipNode = ZDesigner.Designer.RootZipNode
        _ZipLocation = location
        _ZipPath = IO.Path.Combine(_ZipLocation, _SourceZipNode.Text & _SourceZipNode.Type.Extension)
        _ZipMaker = New ZipMaker(_SourceZipNode, filters)
        ShowZipHeading()
        _ZipMaker.MakeZip()
    End Sub
    Private _ZipPath As String = ""
    'Making Events :
    Private Sub _ZipMaker_ZipMakeStarted(sender As Object, e As ZipMakingEventArgs) Handles _ZipMaker.ZipMakeStarted
        Dim status As String = String.Format("Loading ({0:P02})", e.MakePercent)
        KL_Status.Text = String.Format("{0} : {1}/{2} - {3}/{4}", _
                                 status, e.AddedEntries, e.TotalEntries, _
                                 StringFromSize(e.AddedSize, 2), StringFromSize(e.TotalSize, 2))
    End Sub
    Private Sub _ZipMaker_ZipMakeUpdated(sender As Object, e As ZipMakingEventArgs) Handles _ZipMaker.ZipMakeUpdated
        Dim status As String = String.Format("Loading ({0:P02})", e.MakePercent)
        KL_Status.Text = String.Format("{0} : {1}/{2} - {3}/{4}", _
                                 status, e.AddedEntries, e.TotalEntries, _
                                 StringFromSize(e.AddedSize, 2), StringFromSize(e.TotalSize, 2))
    End Sub
    Private Sub _ZipMaker_ZipMakeFinished(sender As Object, e As ZipMakingEventArgs) Handles _ZipMaker.ZipMakeFinished
        Dim status As String = String.Format("Loaded ({0:P02})", e.MakePercent)
        KL_Status.Text = String.Format("{0} : Total Entries ({1}) - Total Size ({2}) ", _
                                         status, e.AddedEntries, StringFromSize(e.AddedSize, 2))
        KL_TotalEntries.Text = CStr(e.TotalEntries)
        KL_TotalSize.Text = StringFromSize(e.TotalSize)
        _ZipMaker.SaveZip(_ZipLocation)
    End Sub
    'Saving Events :
    Private Sub _ZipMaker_ZipSavingStarted(sender As Object, e As System.EventArgs) Handles _ZipMaker.ZipSavingStarted
        ShowStartingFields()
        KH_Controls.Enabled = True
        Dim status As String = String.Format("Saving...({0:P02})", _ZipMaker.ZipPercent)
        KL_Status.Text = status
    End Sub
    Private Sub _ZipMaker_ZipEntryReady(sender As Object, e As ZipEntryReadyEventArgs) Handles _ZipMaker.ZipEntryReady
        ShowEntryHeading(ZDesigner.Designer.ImagesList.Images.Item(IO.Path.GetExtension(e.EntryFileName)), IO.Path.GetFileName(e.EntryFileName))
        Dim status As String = String.Format("Saving...({0:P02})", _ZipMaker.ZipPercent)
        KL_Status.Text = status
    End Sub
    Private Sub _ZipMaker_ZipSavingUpdated(sender As Object, e As System.EventArgs) Handles _ZipMaker.ZipSavingUpdated
        Me.SuspendLayout()
        ShowZipProgress()
        ShowEntryProgress()
        ShowProgressFields()
        Dim status As String = String.Format("Saving...({0:P02})", _ZipMaker.ZipPercent)
        KL_Status.Text = status
        Me.ResumeLayout()
    End Sub
    Private Sub _ZipMaker_ZipSavingFinished(sender As Object, e As ZipSavingFinishedEventArgs) Handles _ZipMaker.ZipSavingFinished
        KH_Controls.Enabled = True
        Dim compressed As Long = 0L
        For Each fn As FileNode In e.ReplacementFilesNodes
            compressed += fn.Size
        Next
        Dim status As String = String.Format("Saved ({0:P02})", _ZipMaker.ZipPercent)
        KL_Status.Text = status
        KL_Status.Text = String.Format("{0} - Size ({1}) - Size ({2}) - Rate ({3:P02})", _
                                                   status, StringFromSize(_ZipMaker.ZipTotalSize) _
                                                   , StringFromSize(compressed), CDbl(compressed) / CDbl(_ZipMaker.ZipTotalSize))
        RaiseEvent ProgressSavingFinished(Me, e)
        If BSA_OpenWhenFinished.Checked = ButtonCheckState.Checked Then
            Process.Start(New ProcessStartInfo("explorer.exe", _ZipLocation))
        End If
    End Sub
    'Canceled :
    Private Sub _ZipMaker_ZipSavingCanceled(sender As Object, e As System.EventArgs) Handles _ZipMaker.ZipSavingCanceled
        ResetProgressFields()
        KH_Controls.Enabled = False
        RaiseEvent ProgressCancelRequired(Me, Nothing)
    End Sub
    'Error Occured :
    Private Sub _ZipMaker_ZipSavingErrorOccured(sender As Object, e As ZipSavingErrorOccuredEventArgs) Handles _ZipMaker.ZipSavingErrorOccured
        RaiseEvent ProgressSavingError(Me, e)
    End Sub
    'Events :
    Public Event ProgressSavingError As EventHandler(Of ZipSavingErrorOccuredEventArgs)
    Public Event ProgressSavingFinished As EventHandler(Of ZipSavingFinishedEventArgs)
    Public Event ProgressCancelRequired As EventHandler(Of EventArgs)
    Public Event ProgressAbortRequired As EventHandler(Of EventArgs)
    'Actions :
    Private Sub BSA_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles BSA_Cancel.Click
        Dim dr As DialogResult = KryptonMessageBox.Show("Are You Sure You Want To Cancel Zip Operations ?" _
                                                        & vbNewLine & "You Will Be Redirected To Zip Designer If Yes." _
                                                        , "Cancel Zip Operations", _
                                                        MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = DialogResult.Yes Then
            _ZipMaker.CancelZip()
        End If
    End Sub
    Private Sub BSA_Abort_Click(sender As System.Object, e As System.EventArgs) Handles BSA_Abort.Click
        Dim dr As DialogResult = KryptonMessageBox.Show("Are You Sure You Want To Abort Zip Operations ?" _
                                                        & vbNewLine & "This Will Close Zipper." _
                                                        , "Abort Zip Operations", _
                                                        MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = DialogResult.Yes Then
            _ZipMaker.CancelZip()
            RaiseEvent ProgressAbortRequired(Me, Nothing)
        End If
    End Sub

End Class
